<?php

namespace app\admin\model\base;

use think\Model;
use think\Cache;
class Source extends Model
{
    // 表名
    protected $name = 'base_source';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'type_text',
        'publish_text',
        'z_publish_text',
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'),'2'=>__('Type 2')];
    }     


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
     public function getPublishTextAttr($value, $data)
    {
   
        $value = $value ? $value : $data['publish'];
         if(!$userlist = Cache::get('USERLIST'))
        {
             $userlist = collection(model(GetTableModel('Admin'))::field("userid,username,gender,isleader,department,delete_status")->select())->toArray();
            $userlist = array_column($userlist,null,'userid');
            Cache::set('USERLIST',$userlist,604800);//有效期一个星期
        }
        return isset($userlist[$value]) ? $userlist[$value]['username']:$value.' 已离职';
    }
    protected function setPublishAttr($value)
    {
        return \think\Session::get('admin')['userid'];
    } 
     public function getZPublishTextAttr($value, $data)
    {
   
        $value = $value ? $value : $data['z_publish'];
         if(!$userlist = Cache::get('USERLIST'))
        {
             $userlist = collection(model(GetTableModel('Admin'))::field("userid,username,gender,isleader,department,delete_status")->select())->toArray();
            $userlist = array_column($userlist,null,'userid');
            Cache::set('USERLIST',$userlist,604800);//有效期一个星期
        }
        return isset($userlist[$value])?$userlist[$value]['username']:$value.' 已离职';
    }
    protected function setZPublishAttr($value)
    {
        return \think\Session::get('admin')['userid'];
    } 




}
